<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>言之翼</title>
    <meta name="description" content="言之翼">
    <meta name="keywords" content="言之翼">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../../plugins/elementui/index.css">
    <link rel="stylesheet" href="../../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../../css/style.css">
</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>系统设置
            <small>角色管理</small>
        </h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>系统设置</el-breadcrumb-item>
            <el-breadcrumb-item>角色管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">
            <div class="filter-container">
                <el-input placeholder="角色名称" v-model="pagination.queryString" style="width: 200px;"
                          class="filter-item"></el-input>
                <el-button @click="search()" class="dalfBut">查询</el-button>
                <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
            </div>
            <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                <el-table-column type="index"  label="序号" align="center"></el-table-column>
                <el-table-column prop="id" label="角色编码" align="center"></el-table-column>
                <el-table-column prop="name" label="角色名称" align="center"></el-table-column>
                <el-table-column prop="level" label="角色等级" align="center"></el-table-column>
                <el-table-column prop="description" label="角色描述" align="center"></el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                        <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination-container">
                <el-pagination
                        class="pagiantion"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        layout="total, prev, pager, next, jumper"
                        :total="pagination.total">
                </el-pagination>
            </div>
            <!-- 新增标签弹层 -->
            <div class="add-form">
                <el-dialog title="新增角色" :visible.sync="dialogFormVisible" width="70%">
                    <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right"
                             label-width="100px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="角色名称" prop="name">
                                    <el-input v-model="formData.name"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="角色等级" prop="level">
                                    <template>
                                        <el-select v-model="formData.level" placeholder="请选择">
                                            <el-option
                                                    v-for="item in options"
                                                    :label="item.label"
                                                    :value="item.value">
                                            </el-option>
                                        </el-select>
                                    </template>
                                </el-form-item>
                            </el-col>
                        </el-row>

                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="选择权限">
                                    <template>
                                        <el-transfer
                                                v-model="permissionIds"
                                                :props="{key: 'id',label: 'name'}"
                                                :titles="['权限列表', '已选列表']"
                                                :data="permissionAllList">
                                        </el-transfer>
                                    </template>
                                </el-form-item>
                            </el-col>
                        </el-row>

                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="菜单选择">
                                    <el-tree
                                            :data="menus"
                                            show-checkbox
                                            node-key="id"
                                            ref="tree"
                                            :default-checked-keys="childMenus"
                                            :props="defaultProps"
                                            @check-change="handleCheckChange">
                                    </el-tree>
                                </el-form-item>
                            </el-col>
                        </el-row>

                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="描述">
                                    <el-input v-model="formData.description" type="textarea"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="handleAdd()">确定</el-button>
                    </div>
                </el-dialog>
            </div>

            <!-- 编辑标签弹层 -->
            <div class="add-form">
                <el-dialog title="编辑角色" :visible.sync="dialogFormVisible4Edit" width="70%">
                    <el-form ref="dataEditForm" :model="formData" :rules="rules" label-position="right"
                             label-width="100px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="角色名称" prop="name">
                                    <el-input v-model="formData.name"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="角色等级" prop="level">
                                    <template>
                                        <el-select v-model="formData.level" placeholder="请选择">
                                            <el-option
                                                    v-for="item in options"
                                                    :key ="item.value"
                                                    :label="item.label"
                                                    :value="item.value">
                                            </el-option>
                                        </el-select>
                                    </template>
                                </el-form-item>
                            </el-col>
                        </el-row>

                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="选择权限">
                                    <template>
                                        <el-transfer
                                                v-model="permissionIds"
                                                :props="{key: 'id',label: 'name'}"
                                                :titles="['权限列表', '已选列表']"
                                                :data="permissionAllList">
                                        </el-transfer>
                                    </template>
                                </el-form-item>
                            </el-col>
                        </el-row>

                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="菜单选择">
                                    <el-tree
                                            :data="menus"
                                            show-checkbox
                                            default-expand-all
                                            node-key="id"
                                            ref="tree"
                                            :default-checked-keys="childMenus"
                                            :props="defaultProps"
                                            @check-change="handleCheckChange">
                                    </el-tree>
                                </el-form-item>
                            </el-col>
                        </el-row>

                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="描述">
                                    <el-input v-model="formData.description" type="textarea"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                        <el-button type="primary" @click="handleEdit()">确定</el-button>
                    </div>
                </el-dialog>
            </div>
        </div>
    </div>
</div>
</body>
<!-- 引入组件库 -->
<script src="../../js/vue.js"></script>
<script src="../../plugins/elementui/index.js"></script>
<script type="text/javascript" src="../../js/jquery.min.js"></script>
<script src="../../js/axios-0.18.0.js"></script>


<script>

    var vue = new Vue({
        el: '#app',
        data: {
            pagination: {//分页相关模型数据
                currentPage: 1,//当前页码
                pageSize: 10,//每页显示的记录数
                total: 0,//总记录数
                queryString: null//查询条件
            },
            dataList: [],//当前页要展示的分页列表数据
            formData: {},//表单数据
            dialogFormVisible: false,//增加表单是否可见
            dialogFormVisible4Edit: false,//编辑表单是否可见
            rules: {//校验规则
                name: [{required: true, message: '角色名称为必填项', trigger: 'blur'}],
                level: [{required: true, message: '角色等级为必填项', trigger: 'blur'}]
            },
            permissionAllList: [],
            permissionIds: [],
            menus:[],//菜单列表
            defaultProps: {
                children: 'children',
                label: 'name'
            },
            selectMenus:[],//选中的菜单列表
            parentMenus:[],//选中的父菜单列表
            childMenus:[],//选中的子菜单列表
            options: [{
                    value: 1,
                    label: '等级1'
                }, {
                    value: 2,
                    label: '等级2'
                }, {
                    value: 3,
                    label: '等级3'
                }, {
                    value: 4,
                    label: '等级4'
                }],

        },
        //钩子函数，VUE对象初始化完成后自动执行
        created() {
            this.findPage();//初始化时，加载列表
            //获取所有的权限数据
            axios.get("/wpermission/findAll").then((res)=>{
                this.permissionAllList = res.data.data;
            });

            //默认获取所有的菜单 分层显示
            axios.get("/wmenu/findAll").then((res)=>{
                this.menus = res.data.data;
            });

        },
        methods: {
            search(){
                this.pagination.currentPage=1;
                this.findPage();
            },
            handleCheckChange(){
                //获取所有选中的节点
                this.childMenus = this.$refs.tree.getCheckedKeys();
                this.parentMenus = this.$refs.tree.getHalfCheckedKeys();
                this.selectMenus = this.parentMenus.concat(this.childMenus);
            },
            //分页查询
            findPage() {
                //分页参数
                var param = {
                    currentPage: this.pagination.currentPage,
                    pageSize: this.pagination.pageSize,
                    name: this.pagination.queryString
                }
                //发送请求
                axios.post("/wrole/searchPage", param).then((res) => {
                    //为模型数据赋值，基于VUE的双向绑定展示到页面
                    console.log(res.data.data.rows);
                    this.pagination.total = res.data.data.total;
                    this.dataList = res.data.data.rows;
                })
            },
            //切换页码
            handleCurrentChange(currentPage) {
                this.pagination.currentPage = currentPage;
                this.findPage();
            },
            // 重置表单
            resetForm() {
                this.formData = {};
            },

            // 弹出添加窗口
            handleCreate() {
                this.resetForm();
                this.dialogFormVisible = true;
                this.permissionIds =[];
                this.selectMenus=[];
            },

            //添加
            handleAdd() {
                this.$refs["dataAddForm"].validate((valid) => {
                    if (valid) {
                        axios.post("/wrole/add?permissionIds="+this.permissionIds+"&menus="+this.childMenus,
                            this.formData).then((res) => {
                            if (res.data.flag) {
                                this.dialogFormVisible = false;
                                this.findPage();
                                this.$message.success(res.data.message);
                            } else {
                                this.$message.error(res.data.message);
                            }
                        })
                    }
                })

            },

            // 弹出编辑窗口
            handleUpdate(row) {
                this.resetForm();
                this.dialogFormVisible4Edit = true;
                this.childMenus=[];
                this.permissionIds =[];
                axios.get("/wrole/findById?id=" + row.id).then((res) => {
                    if (res.data.flag) {
                        console.log(res.data);
                        this.formData = res.data.data.role;
                        this.childMenus=res.data.data.menuIds;
                        this.permissionIds = res.data.data.permissionIds;

                    } else {
                        this.$message.error(res.data.message);
                    }
                })
            },
            //编辑
            handleEdit() {
                this.$refs["dataEditForm"].validate((valid) => {
                    if (valid) {
                        axios.post("/wrole/update?permissionIds="+this.permissionIds+"&menus="+this.childMenus,
                            this.formData).then((res) => {
                            if (res.data.flag) {
                                this.dialogFormVisible4Edit = false;
                                this.findPage();
                                this.$message.success(res.data.message);
                            } else {
                                this.$message.error(res.data.message);
                            }
                        })
                    }
                })
            },

            // 删除
            handleDelete(row) {
                this.$confirm("确定要删除吗？", "提示", {type: 'warning'}).then((valid) => {
                    if (valid) {
                        axios.get("/wrole/delete?id=" + row.id).then((res) => {
                            if (res.data.flag) {
                                this.findPage();//刷新列表
                                this.$message.success(res.data.message);
                            } else {
                                this.$message.error(res.data.message);
                            }
                        })
                    }
                })

            }//删除结束
        }


    })
</script>
</html>